import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Button 	= $Lazy.load(()=>import('@antd/button'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// =====================================================================
const width = '20%'
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '开始条码号',	name: 'start_code' },
			{ label: '结束条码号',	name: 'end_code' },
			{ label: '领取人',		name: 'real_name'},
			{ label: '医院名称',		name: 'hosp_id',		type:'select' },
			{ label: '领取时间',		names: ['start_date','end_date'], 	type:'date-range', 	loading:true },
		],
		formData : [
			{ label: '开始条码号',	name: 'start_code', 	width,   mb:0	},
			{ label: '结束条码号',	name: 'end_code', 		width,   mb:0	},
			{ label: '医院名称',		name: 'hosp_id',	width, type:'select', mb:0, loading:true}
		],
	}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 医院名称
			const _data = await f.getCache({ api:'bs-hospital/select' })
			$fn.setRow( forms, 'hosp_id', _data )
			$fn.setRow( formData, 'hosp_id', _data )
			
			this.setState({ forms, formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'barcode/receiptlist',{isIndex:true})
	controls = ()=> []
	
	// 表单
	cols = [
		{ title: '开始条码', 	dataIndex: 'start_code', 	...$fn.table.spec_code },
		{ title: '结束条码', 	dataIndex: 'end_code', 		...$fn.table.spec_code },
		{ title: '领取人', 		dataIndex: 'real_name', 	...$fn.table.name },
		{ title: '数量', 		dataIndex: 'num', 			...$fn.table.num },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 	width:200 },
		{ title: '领用时间', 	dataIndex: 'created_at',	...$fn.table.time},
	]
	
	render(){
		return (
			<>
				<div className='ptb10 fx bbor1'>
					{
						SubmitForm.call(this,{ 
							labelWidth	: 's6', 
							api			: 'barcode/receipt', 
							query		: ({ start_code, end_code, hosp_id }) =>  ( { start_code, end_code, hosp_id } ),
							onRefresh	: ({ data, param}) => {
								this.formRef.reset(['start_code','end_code'])
								this.fetch()
							}
						})
					}
					<Button label='领用' size='middle' className='mr20' onClick={()=> {
						this.formRef.submit()
					}} />
				</div>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this, {idStr: 'id', type: -1, isIndex:true, isInit: window.isInit} ) }
			</>
		)
	}
}

export default Index